﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillData();
    }
    void FillData() {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument doc = XDocument.Load(path);
        var student = from st in doc.Element("Students").Elements("Student") select new { 
            ID = st.Element("ID").Value,
            Name = st.Element("Name").Value,
            Address = st.Element("Address").Value
        };
        
        GridView1.DataSource = student;
        GridView1.DataBind();
    }

   
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument doc = XDocument.Load(path);
        var student = doc.Descendants("Students").Single();
        if (student != null)
        {
            XElement el = new XElement("Student");
            el.Add(new XElement("ID", txtID.Text));
            el.Add(new XElement("Name", txtName.Text));
            el.Add(new XElement("Address", txtAddress.Text));
            student.Add(el);
            doc.Save(path);
            FillData();
            SetNullTextbox();
            Response.Write("<script LANGUAGE='JavaScript' >alert('Add Success!!')</script>");
        }
        else
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Add not Success!!')</script>");
        }
       
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex != -1)
        {
            GridViewRow row = GridView1.SelectedRow;
            string Id = HttpUtility.HtmlDecode(row.Cells[1].Text);
            XDocument document = XDocument.Load(Server.MapPath("~/FPT-Aptech2.xml"));
            var deleteQuery = from r in document.Descendants("Student") where r.Element("ID").Value == Id select r;
            deleteQuery.First().Remove();
            document.Save(Server.MapPath("~/FPT-Aptech2.xml"));
            FillData();

        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex != -1)
        {
            GridViewRow row = GridView1.SelectedRow;

            txtID.Text = HttpUtility.HtmlDecode(row.Cells[1].Text);
            txtName.Text = HttpUtility.HtmlDecode(row.Cells[2].Text);
            txtAddress.Text = HttpUtility.HtmlDecode(row.Cells[3].Text);
        }
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex != -1)
        {
            if (btnupdate.Text == "Update")
            {
                btnupdate.Text = "Insert";
                Disable();
                btnupdate.Enabled = true;
                txtID.Enabled = false;
            }
            else if (btnupdate.Text == "Insert")
            {
                if (txtID.Text.Trim() != null && txtName.Text.Trim() != null && txtAddress.Text.Trim() != null)
                {
                    GridViewRow row = GridView1.SelectedRow;
                    string Id = HttpUtility.HtmlDecode(row.Cells[1].Text);
                    XElement xEmp = XElement.Load(Server.MapPath("~/FPT-Aptech2.xml"));
                    var student = from st in xEmp.Elements("Student") where st.Element("ID").Value == Id select st;
                    student.First().Element("Name").Value = txtName.Text.Trim();
                    student.First().Element("Address").Value = txtAddress.Text.Trim();
                    xEmp.Save(Server.MapPath("~/FPT-Aptech2.xml"));
                    FillData();
                    Enable();
                    btnupdate.Text = "Update";
                    txtID.Enabled = true;
                    SetNullTextbox();
                }
                else
                {
                    txtName.Text = "";
                    txtAddress.Text = "";
                    txtName.Focus();
                }
            }
        }
    }
    protected void Enable()
    {
        btnAdd.Enabled = true;
        btnDelete.Enabled = true;
        btnupdate.Enabled = true;
        GridView1.Enabled = true;

    }
    protected void Disable()
    {
        btnAdd.Enabled = false;
        btnDelete.Enabled = false;
        btnupdate.Enabled = false;
        GridView1.Enabled = false;

    }
    protected void SetNullTextbox()
    {
        txtID.Text = "";
        txtName.Text = "";
        txtAddress.Text = "";
       
    
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument doc = XDocument.Load(path);
        if (DropDownList1.SelectedItem.Value == "ID")
        {
            SearchByID(doc);
        }
        else if (DropDownList1.SelectedItem.Value == "Name")
        {
            SearchByName(doc);
        }
    }

    private void SearchByName(XDocument doc)
    {
        string Id = txtSearch.Text.Trim();
        var SearcdID = from student in doc.Element("Students").Elements("Student")
                       where student.Element("Name").Value == Id
                       select new
                       {
                           ID = student.Element("ID").Value,
                           Name = student.Element("Name").Value,
                           Address = student.Element("Address").Value
                       };

        GridView1.DataSource = SearcdID.ToList();
        GridView1.DataBind();
    }

    private void SearchByID(XDocument doc)
    {
        string Id = txtSearch.Text.Trim();
        var SearcdID = from student in doc.Element("Students").Elements("Student")
                       where student.Element("ID").Value == Id
                       select new
                       {
                           ID = student.Element("ID").Value,
                           Name = student.Element("Name").Value,
                           Address = student.Element("Address").Value
                       };

        GridView1.DataSource = SearcdID.ToList();
        GridView1.DataBind();
    }
    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        FillData();
    }
}